package cn.linjianhui.cluster.Controller;

import cn.linjianhui.cluster.service.UserRedPacketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/userRedPacket")
public class UserRedPacketController {
    @Autowired
    private UserRedPacketService userRedPacketService;

//    @GetMapping("/")
//    public String index() {
//        return "index";
//    }

    @GetMapping("/grapRedPacket")
    public Map<String, Object> grapRedPacket(Long redPacketId, Long userId) {
        int result = userRedPacketService.grabRedPacket(redPacketId, userId);

        // 使用Redis抢红包
//        int result = userRedPacketService.grabRedPacketByRedis(redPacketId, userId);

        Map<String, Object> retMap = new HashMap<>();
        boolean flag = result > 0;
        retMap.put("success", flag);
        retMap.put("message", flag?"抢红包成功":"抢红包失败");
        return retMap;
    }
}
